﻿using System;
using System.Collections;
using System.Data;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for ReferanceUserDB
/// </summary>
/// 
namespace CE.User
{
    public class ReferenceUserDB
    {
        public ReferenceUserDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(ReferenceUser referenceuser)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocReferenceUserDetailInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@RefCEUserId", referenceuser.RefCEUserId);
            _sPara.Add("@ProductType", referenceuser.ProductType);
            _sPara.Add("@ProductId", referenceuser.ProductId);
            _sPara.Add("@Email", referenceuser.Email);
            _sPara.Add("@FullName", referenceuser.FullName);
            _sPara.Add("@NRIC", referenceuser.NRIC);
            _sPara.Add("@VehicleNumber", referenceuser.VehicleNumber);
            _sPara.Add("@MobileNumber", referenceuser.MobileNumber);
            _sPara.Add("@Info1", referenceuser.Info1);
            _sPara.Add("@Status", referenceuser.Status);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public ReferenceUser GetReferenceUser(Int64 RefCEUserId, Int64 ProductId, string ProductType)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocReferenceUserDetailSelectSingleItem";
            ReferenceUser _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@RefCEUserId", RefCEUserId);
            _sPara.Add("@ProductId", ProductId);
            //_sPara.Add("@ProductType", ProductType);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new ReferenceUser();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        private ReferenceUser FillDataRecord(DataTable myDataTable)
        {
            ReferenceUser myReferenceUser = new ReferenceUser();
            myReferenceUser.UserId = Convert.ToInt64(myDataTable.Rows[0]["UserId"].ToString());
            myReferenceUser.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myReferenceUser.ProductType = myDataTable.Rows[0]["ProductType"].ToString();
            myReferenceUser.ProductId = Convert.ToInt64(myDataTable.Rows[0]["ProductId"].ToString());
            myReferenceUser.Email = myDataTable.Rows[0]["Email"].ToString();
            myReferenceUser.FullName = myDataTable.Rows[0]["FullName"].ToString();
            myReferenceUser.NRIC = myDataTable.Rows[0]["NRIC"].ToString();
            myReferenceUser.VehicleNumber = myDataTable.Rows[0]["VehicleNumber"].ToString();
            myReferenceUser.MobileNumber = myDataTable.Rows[0]["MobileNumber"].ToString();
            myReferenceUser.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            myReferenceUser.Info1 = myDataTable.Rows[0]["Info1"].ToString();
            return myReferenceUser;
        }

    }
}